package com.incar.chuxing.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bumptech.glide.Glide;
import com.incar.chuxing.R;
import com.incar.chuxing.activity.MainActivity;
import com.incar.chuxing.activity.WebViewActivity;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.bean.CarListBean.AttachmentBean.ContentBean;
import com.incar.chuxing.bean.TabChangeEvent;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.RxBus;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Michael Pi on 2016/9/5.
 */
public class CarsPagerAdapter extends PagerAdapter {

    /*private TextView item_car_brand;*/

    public boolean isSkip = false;

    private Context context;
    public List<ContentBean> list = new ArrayList<ContentBean>();
    private LayoutInflater inflater = null;
    private Map<String, String> gradePicMap = new HashMap<>();

    public CarsPagerAdapter(Context context, List<ContentBean> carResultList, Map<String, Object> picMap) {
        this.context = context;
        this.gradePicMap = ((Map<String, String>) picMap.get("gradePic"));
        this.list = carResultList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.item_adapter_car, container, false);
        TextView item_car_duration;
        TextView item_car_seats;
        TextView item_rule;
        TextView item_car_distance;
        ImageView item_car, item_icon;
        TextView tv_vp_car_license;
        TextView item_branModel;
        /*item_car_brand = (TextView) view.findViewById(R.id.item_car_brand);*/
        item_car_distance = (TextView) view.findViewById(R.id.item_car_distance);
        item_car_duration = (TextView) view.findViewById(R.id.item_car_duration);
        item_car_seats = (TextView) view.findViewById(R.id.item_car_seats);
        item_car = (ImageView) view.findViewById(R.id.item_car);
        item_icon = (ImageView) view.findViewById(R.id.detail_rank_iv);
        item_rule = (TextView) view.findViewById(R.id.item_rule);
        tv_vp_car_license = (TextView) view.findViewById(R.id.tv_vp_car_license);
        item_branModel = (TextView) view.findViewById(R.id.item_branModel);
        LatLng myPosition = new LatLng(Double.parseDouble(((MyApplication) context.getApplicationContext()).getCurPosition()[0]),
                Double.parseDouble(((MyApplication) context.getApplicationContext()).getCurPosition()[1]));
        LatLng carPosition = new LatLng(list.get(position).getLatitude(), list.get(position).getLongitude());
        item_car_distance.setText(new DecimalFormat("0.0").format(DistanceUtil.getDistance(myPosition, carPosition) / 1000));
        //item_car_brand.setText(((List<MapParksAndCarsBean.AttachmentBean.CarModelsBean>) list).get(position).getBrand() + ((List<MapParksAndCarsBean.AttachmentBean.CarModelsBean>) list).get(position).getModel());
        item_car_duration.setText(list.get(position).getMileage());
        item_car_seats.setText(list.get(position).getSeat() + "");
        tv_vp_car_license.setText(list.get(position).getHideLicenseTag());
        Glide.with(context).load(((MyApplication) context.getApplicationContext()).getImageUrl() + (list).get(position).getPicture()).into(item_car);
        if (gradePicMap != null) {
            Glide.with(context).load(((MyApplication) context.getApplicationContext()).getImageUrl() + gradePicMap.get((list).get(position).getGradePic())).into(item_icon);
        }
        String description = (list).get(position).getGradePic().replace("+", "PLUS").toUpperCase();
        item_branModel.setText((list).get(position).getBrand() + " " + (list).get(position).getModelCN());
        item_rule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, WebViewActivity.class);
                intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, "计价规则");
                if (MainActivity.tabSwitchView.getSelected() == 0) {//共享
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlContainer.Request.h5UrlMap.get(UrlContainer.Request.html_price_shareRent_level) + "?model=" + description);
                } else {
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlContainer.Request.h5UrlMap.get(UrlContainer.Request.html_price_dayRent_level) + "?model=" + description);
                }
                context.startActivity(intent);
            }
        });


        RxBus.getInstance().toObservable(TabChangeEvent.class).subscribe(new Observer<TabChangeEvent>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TabChangeEvent tabChangeEvent) {

                ObjectAnimator animator = ObjectAnimator.ofFloat(item_rule, "rotationY", 0, 180, 360);
                animator.setDuration(1000);
                animator.start();

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void refreshData(List<ContentBean> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
    }

    public ContentBean getItem(int position) {
        return list.get(position);
    }

    /*@Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }*/

}
